using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class TrackGateway
    {
        private SqlConnection connection;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        private SqlCommandBuilder builder;

        private SqlCommand recordingCommand;
        private SqlDataAdapter recordingAdapter;
        private SqlCommandBuilder recordingBuilder;

        public TrackGateway(SqlConnection connection)
        {
            this.connection = connection;
            command = new SqlCommand("SELECT * FROM Track WHERE id=@id", connection);
            command.Parameters.Add("@id", SqlDbType.BigInt);
            adapter = new SqlDataAdapter(command);
            builder = new SqlCommandBuilder(adapter);

            recordingCommand = new SqlCommand("SELECT * FROM Track WHERE recordingid=@recordingid", connection);
            recordingCommand.Parameters.Add("@recordingid", SqlDbType.BigInt);
            recordingAdapter = new SqlDataAdapter(recordingCommand);
            recordingBuilder = new SqlCommandBuilder(recordingAdapter);

        }

        public long Insert(string title, int duration, RecordingDataSet recordingDataSet) {
            RecordingDataSet.TrackRow newRow = recordingDataSet.Track.NewTrackRow();
            newRow.id = IdGenerator.GetNextId(recordingDataSet.Track.TableName, connection);
            newRow.title = title;
            newRow.duration = duration;
            recordingDataSet.Track.AddTrackRow(newRow);
            Update(recordingDataSet);
            return newRow.id;
        }

        public RecordingDataSet.TrackRow FindById(long id, RecordingDataSet recordingDataSet)
        {
            command.Parameters["@id"].Value = id;
            adapter.Fill(recordingDataSet, recordingDataSet.Track.TableName);
            DataRow[] rows = recordingDataSet.Track.Select(String.Format("id={0}", id));
            if(rows.Length<1) return null;
            return ( RecordingDataSet.TrackRow )rows[0];
        }

        public RecordingDataSet.TrackRow[] FindByRecordingId(long recordingid, RecordingDataSet recordingDataSet)
        {
            recordingCommand.Parameters["@recordingid"].Value = recordingid;
            recordingAdapter.Fill(recordingDataSet, recordingDataSet.Track.TableName);
            DataRow[] rows = recordingDataSet.Track.Select(String.Format("recordingid={0}", recordingid));
            return (RecordingDataSet.TrackRow[])rows;
        }

        public void Delete(long deletedId, RecordingDataSet recordingDataSet)
        {
            RecordingDataSet.TrackRow deletedRow = FindById(deletedId, recordingDataSet);
            deletedRow.Delete();
            Update(recordingDataSet);
        }

        public void Update(RecordingDataSet recordingDataSet)
        {
            adapter.Update(recordingDataSet, recordingDataSet.Track.TableName);
        }
    }
}
